<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function login(){
       $this->display();
    }
    public function regedit(){
       $this->display();
    } 

    public function logVert(){
     if(!IS_POST)$this->error("非法请求");
        $user = M('User');
        $name =I('member');    
     
        $password =I('password');
        $rules = array(
         array('member','require','用户名不能为空'),
         array('password','require','密码不能为空'),
         );
        if(!$user->validate($rules)->create()){
        $this->error($user->getError(),U('Index/login'),2) ;}
        $name_Ver1 = $user->where(array('name'=>$name))->find();
        $name_Ver2 = $user->where(array('password'=>$password))->find();
        if(!$name_Ver1) {
            $this->error('该用户账号不存在 :(',U('Index/login'),1) ;
            
        }else if($name_Ver1&&!$name_Ver2){ 
            $this->error('用户名与密码不匹配 :(',U('Index/login'),1) ;
         }else{session('name',$name);
          $data = $user->where(array('name'=>$name,'password'=>$password))->find();
             session('user_id',$data['id']);
            $this->success("已成功登录！更多的惊喜在等着你！！！",U('Index/index'));}
    }

    public function regVert(){
      // var_dump(date('Y-m-d H:i:s'));exit;
        if(!IS_POST)$this->error("非法请求");
        $user = M('User');
      // var_dump($user->select());exit;
        $code = I('vertify','','strtolower');
        $rules = array(

         array('name','require','用户名不能为空'),
         array('psw1','require','密码不能为空'),
         array('name','require','验证码不能为空'),
          //array('img',array(1,2,3),'值的范围不正确！',2,'in'),
         array('psw2','psw1','确认密码不一致',0,'confirm'), 
         array('name','','该用户名已经被注册！',0,'unique',1), 
         array('psw1','/^[A-Za-z0-9]{3,15}$/','密码由3-15位的字母和数字组成'), 
      

        );
       if(!$user->validate($rules)->create()){
            
        $this->error($user->getError(),U('Index/regedit'),2) ;}

        //验证验证码是否正确
        if(!($this->check_verify($code))){
            $this->error('验证码错误',U('Index/regedit'),1);
        }else {
            $data['name']=I('name');
            $data['password']=I('psw1');
           
            $data['register_time']=Date("Y-m-d H:i:s");
            // echo  $data['register_time'];exit;
            $data['img']=1;
            $user->add($data);
            session('name',$name);
         	$this->success("已成功注册！更多的惊喜在等着你！！！",U('Index/regedit'));
         }
        
       
       /* //更新登陆信息
        $data =array(
            'id' => $name['id'],
            'update_at' => time(),
            'login_ip' => get_client_ip(),
        );*/
       

    }

     //验证码
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->codeSet = '0123456789';
        $Verify->fontSize = 16;
        $Verify->length = 3;

        $Verify->entry();
    }

    protected function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }


    //用户退出登录
    public function loginOut(){
        session(null);
    $this->success('退出成功！',U('Index/index'));
    }
  
}
